/**
 * @Controller 模板
 */
const Controller = require('egg').Controller;

const deleteTemplateRule = {
    id: 'string',
};

class TemplatesController extends Controller {
    /**
     * @Router GET /api/v1/templates
     * @Request query number page 当前页（1）
     * @Request query number pageSize 分页大小（10）
     * @Request header string authorization
     * @Response 200 createTemplatesResponse 响应
     * @Summary 获取模板列表
     */
    async index() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.template.findTemplate(ctx.query);
            ctx.body = {
                code: 200,
                data,
                success: true,
                msg: ''
            }
        } catch (err) {
            throw err;
        }
    }

    /**
     * @Router POST /api/v1/templates
     * @Request body createTemplatesRequest createTemplatesRequest 创建模板
     * @Request header string authorization
     * @Response 200 createTemplatesResponse 响应
     * @Summary 创建模板
     */
    async create() {
        const ctx = this.ctx;
        ctx.validate(ctx.rule.createTemplatesRequest, ctx.request.body);
        const data = {
            ...ctx.request.body,
            author: ctx.state.user.id
        }
        try {
            const accessToken = await ctx.service.wx.getAccessToken()
            await ctx.service.security.msgSecCheck(data.description, accessToken);
            const newTarget = await ctx.service.target.createTarget({
                url: ctx.request.body.targetUrl,
                shareType: ctx.request.body.isMiniProgram ? 0 : 1
            });
            if (newTarget) {
                data.target = newTarget._id
            }
            const newTemplate = await ctx.service.template.createTemplate(data);
            ctx.body = {
                code: 200,
                data: newTemplate,
                success: true,
                msg: ``
            };
        }
        catch (err) {
            throw err;
        }

    }

    /**
     * @Router GET /api/v1/templates
     * @Request query string id  模板id
     * @Request header string authorization
     * @Response 200 createTemplatesResponse 响应
     * @Summary 创建模板
     */
    async show() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.template.findTemplateDetail(ctx.params.id);
            const color = await ctx.service.qiniu.getImageAve(data.imgUrl);
            const info = await ctx.service.qiniu.getImageInfo(data.imgUrl);

            ctx.body = {
                code: 200,
                data: { ...data, ...color, ...info },
                success: true,
                msg: ``
            };
        }
        catch (err) {
            throw err;
        }

    }
    /**
     * @Router DELETE /api/v1/templates
     * @Request params number id 删除模板
     * @Response 200 baseResponse 响应
     * @Summary 用户删除模板，并返回删除结果
     */
    async destroy() {
        const { ctx } = this;
        ctx.validate(deleteTemplateRule, ctx.params);
        try {
            const result = await ctx.service.template.deleteTemplate(ctx.params.id);
            if (result && result._id) {
                ctx.body = {
                    code: 200,
                    data: {},
                    success: true,
                    msg: '删除成功'
                }
            } else {
                throw new Error('删除失败')
            }
        } catch (error) {
            throw error;
        }
    }
}

module.exports = TemplatesController;